package wangjg.coco.views;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import wangjg.coco.PDUtil;
import wangjg.coco.common.CharsetDetector;
import wangjg.coco.common.CharsetMain;

public class CharsetDialog extends Dialog {
	private Label	srcCharsetLabel = null;
	private Text	srcCharsetInput = null;
	private Label	destCharsetLabel = null;
	private Text	destCharsetInput = null;
	
	private IStructuredSelection selection;
	
	public CharsetDialog(IShellProvider parentShell) {
		super(parentShell);
	}

	public CharsetDialog(Shell parentShell) {
		super(parentShell);
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		this.setLayout(composite);
		srcCharsetLabel = new Label(composite, SWT.NONE);
		srcCharsetLabel.setText("当前编码");
		srcCharsetInput = new Text(composite, SWT.BORDER);
		this.setGridData(srcCharsetInput, 1);
		
		destCharsetLabel = new Label(composite, SWT.NONE);
		destCharsetLabel.setText("目标编码");
		destCharsetInput = new Text(composite, SWT.BORDER);
		this.setGridData(destCharsetInput, 1);
		
		this.fillData();
		
		Listener srcListen = new Listener(){
			public void handleEvent(Event event) {
				if(event.widget instanceof Text){
					((Text)event.widget).selectAll();
				}
			}
		};
		srcCharsetInput.addListener(SWT.MouseDoubleClick, srcListen);
		srcCharsetInput.addListener(SWT.FocusIn, srcListen);
		destCharsetInput.addListener(SWT.MouseDoubleClick, srcListen);
		destCharsetInput.addListener(SWT.FocusIn, srcListen);
		
		return composite;
	}
	
	private void setLayout(Composite composite){
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.makeColumnsEqualWidth=false;
		composite.setLayout(gridLayout);
	}
	
	private void setGridData(Control control, int rowSpan){
		GridData gd = new GridData();
		gd.horizontalSpan = rowSpan;
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;//?
		control.setLayoutData(gd);
	}
	
	private void fillData(){
		Color red = new Color(Display.getDefault(), 200, 100, 100);
		String projectCharset = PDUtil.getCharset();
		
		try {
			IResource resource = (IResource)this.selection.getFirstElement();
			getShell().setText(resource.getName());
			
			String charset = "";
			String deteCharset = "";
			if(resource instanceof IFile){
				IFile ifile = (IFile)resource;
				charset = ifile.getCharset(true);
				deteCharset = this.deteCharset(ifile.getLocation().toFile(), projectCharset);
			}
			boolean diff = (charset==null||!charset.equals(deteCharset))&&!deteCharset.equals("ASCII");
			if(charset==null){
				charset = deteCharset;
			}else{
				if(diff && deteCharset!=null && deteCharset.length()>0){
					charset = charset+"/"+deteCharset;
				}
			}
			if(charset!=null){
				srcCharsetInput.setText(charset);
			}
			if(diff){
				srcCharsetInput.setForeground(red);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.destCharsetInput.setText(projectCharset);
	}
	private static Map<String,String> compMap=new HashMap<String,String>();
	static{
		compMap.put("GB2312", "GBK");
		compMap.put("GB18030", "GBK");
	}
	private String deteCharset(File file, String expect){
		String charset = null;
		FileInputStream in = null;
		try {
			CharsetDetector detector = new CharsetDetector();
			in = new FileInputStream(file);
			detector.detect(in);
			charset = detector.getResult();
			if(charset!=null && expect!=null && !charset.equals(expect)){
				String temp = compMap.get(charset);
				if(temp!=null){
					charset = temp;
				}
			}
			if(charset==null){
				charset = detector.getProbableSeq();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					if(in!=null)in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return charset;
	}

	protected void okPressed() {
		try {
			IResource resource = (IResource)this.selection.getFirstElement();
			File file = resource.getLocation().toFile();
//			PDUtil._testAST(file);

			String srcCharset = this.srcCharsetInput.getText();
			String destCharset = this.destCharsetInput.getText();
			
			CharsetMain app = new CharsetMain();
			app.setSrcCharset(srcCharset);
			app.setTargetCharset(destCharset);
			app.setPath(file.getPath());
			app.go();
			
			MessageDialog.openInformation(this.getShell(), "转换结果", app.getMessage());
			
			resource.refreshLocal(IResource.DEPTH_ZERO, null);
			
			super.okPressed();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setSelection(IStructuredSelection selection) {
		this.selection = selection;
	}
}